package com.meiyou.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.meiyou.pojo.MeiyouUser;
import com.meiyou.pojo.WXUserInfo;
import com.meiyou.service.AddProjectService;
import com.meiyou.service.AdminService;
import com.meiyou.service.AppSettingService;
import com.meiyou.service.ShareService;
import com.meiyou.service.TecentService;
import com.meiyou.service.ThirdService;
import com.meiyou.service.UserService;
import com.meiyou.utils.Context;
import com.meiyou.utils.Msg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "美游app控制器", tags = { "所有api接口" })
public class MeiyouController {

	@Autowired
	AddProjectService addproject;
	@Autowired
	AppSettingService appsetting;
	@Autowired
	AdminService admin;
	@Autowired
	UserService userService;
	@Autowired
	ThirdService third;
	@Autowired
	TecentService tencentService;
	@Autowired
	ShareService shareService;
	
	
	
	/**
	 * 找回密码
	 */
	@RequestMapping(value="retrievePassword",method=RequestMethod.GET)
	@ApiOperation("找回密码")
	public @ResponseBody Msg retrievePassword(String phone,String code,String password,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute(phone)==null) {
		  msg = Msg.fail();
		  msg.setMsg("请先发送验证码");
		  return msg;
		}else {
			if(req.getSession().getAttribute(phone).toString().equals(code)) {
				
				MeiyouUser user = userService.selUserByPhone(phone);
				if(user==null) {
					msg = Msg.fail();
					msg.setCode(400);
					msg.setMsg("该手机号未注册");
					return msg;
				}else {
					Msg nmsg = userService.updPwdByUccount(password, user.getuCount());
					if(nmsg.getCode()==100) {
						msg = Msg.success();
						return msg;
					}else {
						msg = Msg.fail();
						msg.setCode(500);
						msg.setMsg("更新密码失败");
						return msg;
					}
				}
				
			}else {
				msg = Msg.fail();
				msg.setCode(300);
				msg.setMsg("验证码不正确");
				return msg;
			}
		}
	
	}
	
	
	/**
	 * 微信登录手机注册
	 */
	@RequestMapping(value="registWxlogin",method=RequestMethod.POST)
	@ApiOperation(value="微信手机号注册")
	public @ResponseBody Msg registWxlogin(String code,String phone,String password,String sharecode,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")==null) {
			msg = Msg.fail();
			msg.setMsg("请先登录");
		}else {
			if(req.getSession().getAttribute(phone)==null) {
				msg = Msg.fail();
				msg.setCode(300);
				msg.setMsg("请先获取验证码");
			}else {
				if(!req.getSession().getAttribute(phone).toString().equals(code)) {
					msg = Msg.fail();
					msg.setCode(400);
					msg.setMsg("验证码错误");
				
				}else {
					List<MeiyouUser> users = userService.selPwdByCount(req.getSession().getAttribute("user").toString());
					if(users.size()==0||users==null) {
						msg = Msg.fail();
						msg.setCode(500);
						msg.setMsg("不存在此用户");
					}else {
						MeiyouUser user = users.get(0);
						if(!userService.selCountByPhone(phone)) {
							if(userService.updPhoneAndPwd(password, phone, req.getSession().getAttribute("user").toString())) {
						    	if(sharecode!=null) {
						    		shareService.selShareCode(sharecode).getCode();
						    	}
						    	msg = Msg.success();
						    }else {
						    	msg= Msg.fail();
						    	msg.setCode(600);
						    	msg.setMsg("更新手机号资料失败");
						    }	
						}else {
							msg = Msg.fail();
							msg.setCode(700);
							msg.setMsg("该手机号已被注册");
						}
					    
					}
				}
			}
		}
		return msg;
	}
	
	
	
	/**
	 * 人脸对比
	 */
	@RequestMapping(value="faceDuibi",method=RequestMethod.POST,consumes="multipert/*",headers = "content-type=multipart/form-data")
	@ApiOperation(value="人脸对比" )
	public @ResponseBody Msg faceDuibi(MultipartFile file,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")==null) {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}else {
			String img = userService.selUserimg(req.getSession().getAttribute("user").toString());
			if(img==null) {
				msg = Msg.fail();
				msg.setMsg("用户没有设置头像");
				return msg;
			}else {	             
				return userService.faceDuibi(file, img);
			} 
		}
	}
	
	/**
	 * 支付密码设置
	 */
	@RequestMapping(value="setPayPwd",method=RequestMethod.POST)
	@ApiOperation("支付密码设置")
	public @ResponseBody Msg setPayPwd(String newPwd,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")!=null) {
			
			//修改密码操作
			return userService.updPayPwdByUccount(newPwd, req.getSession().getAttribute("user").toString());
			
			
		}else {
			//未登录操作
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	}
	
	
	/**
	 * 账号密码设置
	 */
	@RequestMapping(value="setCountPwd",method=RequestMethod.POST)
	@ApiOperation("账号密码设置")
	public @ResponseBody Msg setCountPwd(String newPwd,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")!=null) {
			
			//修改密码操作
			return userService.updPwdByUccount(newPwd, req.getSession().getAttribute("user").toString());
		}else {
			//未登录操作
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	}
	
	/**
	 * 修改个人资料
	 * @param user
	 * @param req
	 * @return
	 */
	@RequestMapping(value="modifyInfo",method=RequestMethod.POST)
	@ApiOperation(value="修改资料" )
	public @ResponseBody Msg modifyInfo(String nickname,int old,String sex,String qianming,HttpServletRequest req) {
		//System.out.println("用户修改资料");
		Msg msg;
		if(req.getSession().getAttribute("user")==null) {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}else {
			MeiyouUser user = new MeiyouUser();
			if(nickname!=null)
			user.setuCount(req.getSession().getAttribute("user").toString());
			user.setuOld(old);
			user.setQianming(qianming);
			user.setuSex(sex);
			user.setuName(nickname);
			if(userService.updUserinfo(user)) {
				Msg msg1=tencentService.setUserInfo(user);
				if((msg1.getCode()==100)) {
					msg = Msg.success();
					return msg;
				}else {
					return msg1;
				}
				
			}else {
				msg = Msg.fail();
				return msg;
			}
		}
	}
	
	/**
	 * 修改头像
	 * @param user
	 * @param req
	 * @return
	 */
	@RequestMapping(value="modifyInfoimg",method=RequestMethod.POST)
	@ApiOperation(value="修改头像" )
	public @ResponseBody Msg modifyInfoimg(MultipartFile img,HttpServletRequest req) {
		Msg msg;
		
		if(req.getSession().getAttribute("user")==null) {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}else {
			String newpath = req.getSession().getServletContext().getRealPath("/")+"imgs/uimg/";
			String filename = System.currentTimeMillis()+img.getOriginalFilename();
			try {
				img.transferTo(new File(newpath,filename));
				String imgurl = Context.fwqaddress+"/"+"imgs/uimg/"+filename;
				
				MeiyouUser user = new MeiyouUser();
				user.setuCount(req.getSession().getAttribute("user").toString());
				user.setuImg(imgurl);
				if(userService.updUserinfoimg(user)) {
					Msg msg1=tencentService.setUserInfo(user);
					if((msg1.getCode()==100)) {
						msg = Msg.success();
						return msg;
					}else {
						return msg1;
					}
				}else {
					msg = Msg.fail();
					return msg;
				}
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		msg =Msg.fail();

		return msg;
		

	}
	
	 
	

	@RequestMapping(value="isLogin",method=RequestMethod.GET)
	@ApiOperation(value="判单用户是否登录")
	public @ResponseBody Msg isLogin(HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")==null) {
			System.out.println("session值位空");
			msg = Msg.fail();
		}else {
			System.out.println("session值"+req.getSession().getAttribute("user"));
			msg = Msg.success();
			msg.add("account", req.getSession().getAttribute("user"));
		}
		return msg;
	}
    
	
	/**
	 * 手机号密码登录
	 */
	@RequestMapping(value="loginByphonepwd",method=RequestMethod.POST)
	@ApiOperation(value="手机号和密码登录")
	public @ResponseBody Msg loginByphonepwd(String phone,String pwd,HttpServletRequest req,Double jindu,Double weidu) {
		Msg msg;
		List<MeiyouUser> list = userService.selUserByPhonePwd(phone, pwd, jindu, weidu);
		if(list.size()==0) {
			msg = Msg.fail();
			msg.setMsg("用户名或密码错误");
			
			return msg;
		}
		else if(list.size()==1){
			msg = Msg.success().add("user", list.get(0)); 
		    req.getSession().setAttribute("user", list.get(0).getuCount());
			return msg;
		}else {
			msg = Msg.fail();
			msg.setMsg("请注意！手机用户重复");
			return msg;
		}
	}
	
	/**
	 * 判断密码是否为空
	 */
	@RequestMapping(value="pwdisSet",method=RequestMethod.GET)
	@ApiOperation(value="判断用户密码是否为空")
	public @ResponseBody boolean pwdIsSet(String ucount) {
		

		List<MeiyouUser> list = userService.selPwdByCount(ucount);
		if(list.size()==1) {
		    MeiyouUser user = list.get(0);
		    if(user.getuPassword()==null) {
		    	return true;
		    }else {
		    	
		    	return false;
		    }
		}else if(list.size()>1){
			
			return false;
		}else {
		
			return false;
		}			
	}
	 
	
	@RequestMapping(value="reSetPwd",method=RequestMethod.POST)
	@ApiOperation(value="验证码修改密码")
	public @ResponseBody Msg reSetPwd(String phone,String code,String newpwd,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute(phone)==null) {
			msg = Msg.fail();
			msg.setMsg("未发送验证码");
			return msg;
		}else {
			if(req.getSession().getAttribute(phone).toString().equals(code)) {
				
				if(userService.updPwd(newpwd, phone)) {
					msg = Msg.success();
					return msg;
				}
				else {
					msg = Msg.fail();
					return msg;
				}
			}
			else {
				msg=Msg.fail();
				msg.setMsg("验证码错误");
				return msg;
			}
		}
		
	}
	
	
	
	
  
	/**
	 * 发布约会项目
	 * @return
	 */
	@RequestMapping(value = "addAppointment", method = RequestMethod.POST)
	@ApiOperation(value = "发布约会项目")
	public @ResponseBody Map<String, String> addAppointment(String jindu, String weidu, String context, String date,
			String starttime, String endtime, int num, int meijin, boolean ispingtai, String ucount,
			@RequestParam("img") CommonsMultipartFile img, String address, HttpServletRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		if (addproject.insAppointment(jindu, weidu, context, date, starttime, endtime, num, meijin, ispingtai, ucount,img, address, req)) {
			map.put("state", "1");
			map.put("message", "发布项目成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "服务器错误");
		return map;
	}

	/**
	 * 获取置顶费
	 */
	@RequestMapping(value = "getZhiDingFei", method = RequestMethod.GET)
	@ApiOperation(value = "获取置顶费")
	public @ResponseBody String getZhiDingFei() {
		return appsetting.selZhiDingfei();
	}

	/**
	 * 计算置顶费
	 * @param tZdtime
	 * @return
	 */
	@RequestMapping(value = "ZhiDingfei", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "计算置顶费")
	public Map<String, Object> zdTuiNa(int tZdtime){
		int zhidingfei= Integer.parseInt(appsetting.selZhiDingfei());
		int ZhiDingfei=zhidingfei*tZdtime;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ZhiDingfei", ZhiDingfei);
		return map;
	} 
	
	/**
	 * 置顶费修改
	 * @param num
	 * @return
	 */
	@RequestMapping(value = "setZhiDingFei", method = RequestMethod.POST)
	@ApiOperation(value = "置顶费设置")
	public @ResponseBody Map<String, String> setZhiDingFei(String num, HttpServletRequest req) {
		System.out.println("设置置顶费"+num);
		Map<String, String> map = new HashMap<String, String>();
		if (appsetting.updZhiDingfei(num, req)) {
			map.put("state", "1");
			map.put("message", "修改成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "修改失败");
		return map;
	}

	/**
	 * 获取安卓更新地址
	 */
	@RequestMapping(value = "androidUrl", method = RequestMethod.GET)
	@ApiOperation(value = "获取android下载地址")
	public @ResponseBody Map<String, String> getAndroidUrl() {
		Map<String, String> map = new HashMap<String, String>();
		String url = appsetting.selUrlForAndroid();
		map.put("url", url);
		return map;
	}

	/**
	 * 更新安卓下载地址
	 * 
	 * @param url
	 * @return
	 */
	@RequestMapping(value = "setAndroidUrl", method = RequestMethod.POST)
	@ApiOperation(value = "更新android下载地址")
	public @ResponseBody Map<String, String> setAndroidUrl(String url, HttpServletRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		if (appsetting.updUrlForAndroid(url, req)) {
			map.put("state", "1");
			map.put("message", "修改成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "修改失败");
		return map;
	}

	/**
	 * 获取苹果下载地址
	 */
	@RequestMapping(value = "IphoneUrl", method = RequestMethod.GET)
	@ApiOperation(value = "获取iphone下载地址")
	public @ResponseBody Map<String, String> getIphoneUrl() {
		Map<String, String> map = new HashMap<String, String>();
		String url = appsetting.selUrlForIphone();
		map.put("url", url);
		return map;
	}

	/**
	 * 更新安卓下载地址
	 * 
	 * @param url
	 * @return
	 */
	@RequestMapping(value = "setIphoneUrl", method = RequestMethod.POST)
	@ApiOperation(value = "更新iphone下载地址")
	public @ResponseBody Map<String, String> setIphoneUrl(String url, HttpServletRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		if (appsetting.updUrlForIphone(url, req)) {
			map.put("state", "1");
			map.put("message", "修改成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "修改失败");
		return map;
	}

	/**
	 * 管理员登录后台
	 */
	@RequestMapping(value = "adminLogin", method = RequestMethod.POST)
	@ApiOperation(value = "管理员后台登录")
	public @ResponseBody Map<String, String> adminLogin(String uname, String upwd, HttpServletRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		if (admin.seltryLogin(uname, upwd, req)) {
			map.put("state", "1");
			map.put("message", "登录成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "登录失败");
		return map;
	}

	/**
	 * 获取诚意金
	 */
	@RequestMapping(value = "getChenYiJin", method = RequestMethod.GET)
	@ApiOperation(value = "获取诚意金金额")
	public @ResponseBody Map<String, String> getChenYiJin() {
		Map<String, String> map = new HashMap<String, String>();
		String chenyijin = appsetting.selChenYiJin();
		map.put("chenyijin",chenyijin);
		return map;
	}
    /**
     * 更新诚意金
     */
	@RequestMapping(value = "updChenYiJin", method = RequestMethod.POST)
	@ApiOperation(value = "更新诚意金金额")
	public @ResponseBody Map<String,String> updChenYiJin(String num,HttpServletRequest req){
		Map<String, String> map = new HashMap<String, String>();
		if (appsetting.updChenYiJin(num, req)) {
			map.put("state", "1");
			map.put("message", "修改成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "修改失败");
		return map;
	}
	/**
	 * 获取官方电话
	 */
	@RequestMapping(value = "getTelPhone", method = RequestMethod.GET)
	@ApiOperation(value = "获取官方电话")
	public @ResponseBody Map<String,String> getTelPhone(){
		Map<String, String> map = new HashMap<String, String>();
		String phone = appsetting.selTelPhone();
		map.put("phone",phone);
		return map;
	}
	
	
	/**
	 * 更新官方电话
	 */
	@RequestMapping(value="updTelPhone",method=RequestMethod.POST)
	@ApiOperation(value = "更新官方电话")
	public @ResponseBody Map<String,String> updTelPhone(String phone,HttpServletRequest req){
		Map<String, String> map = new HashMap<String, String>();
		if (appsetting.updTelPhone(phone, req)) {
			map.put("state", "1");
			map.put("message", "修改成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "修改失败");
		return map;
	}
	/**
	 * 获取平台发布金
	 * @return
	 */
	@RequestMapping(value="getFaBujin",method=RequestMethod.GET)
	@ApiOperation(value = "获取平台发布金")
	public @ResponseBody Map<String,String> getFaBujin(){
		Map<String, String> map = new HashMap<String, String>();
		String num = appsetting.selFaBuJin();
		map.put("num",num);
		return map; 
	}
	
	/**
	 * 更新平台发布金
	 */
	@RequestMapping(value="updFaBujin",method=RequestMethod.POST)
	@ApiOperation(value = "更新平台发布金")
	public @ResponseBody Map<String,String> setFaBuJin(String num,HttpServletRequest req){
		Map<String,String> map = new HashMap<String,String>();
		if(appsetting.updFaBuJin(num, req)) {
			map.put("state", "1");
			map.put("message", "修改成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "修改失败");
		return map;		
	}
	
	/**
	 * 获取提现手续费
	 */
	@RequestMapping(value="getTiXian",method=RequestMethod.GET)
	@ApiOperation(value="获取提现手续费")
	public @ResponseBody Map<String,String> getTiXian(){
		Map<String,String> map = new HashMap<String,String>();
		String num = appsetting.selTiXian();
	    map.put("tixian",num);
	    return map;
	}
	
	/**
	 * 更新提现手续费
	 */
	@RequestMapping(value="updTiXian",method=RequestMethod.POST)
	@ApiOperation(value="更新提现手续费")
	public @ResponseBody Map<String,String> updTiXian(String num,HttpServletRequest req){
		Map<String,String> map = new HashMap<String,String>();
		if(appsetting.updTiXian(num, req)) {
			map.put("state", "1");
			map.put("message", "修改成功");
			return map;
		}
		map.put("state", "0");
		map.put("message", "修改失败");
		return map;		
	}
	/**
	 * 获取用户美金
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getMeijin",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="获取用户美金")
	public Map<String, Object> getMeijin(HttpServletRequest request){
		String uCount = request.getSession().getAttribute("user")+"";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("meijin", userService.selUserByUcount(uCount));
		return map;
	}
	
}
